package com.fary.rabbitmq.topic;

import com.fary.rabbitmq.config.RabbitMQFactory;
import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @author chenyq
 * @date 2023/11/28
 */

public class Consumer2 {

    private static final String TOPIC_EXCHANGE = "topicExchange";
    private static final String TOPIC_QUEUE_1 = "topicQueue_1";
    private static final String TOPIC_QUEUE_2 = "topicQueue_2";

    public static void main(String[] args) {
        // 创建连接工厂
        ConnectionFactory factory = RabbitMQFactory.getConnectionFactory();
        // 创建连接
        Connection connection = null;
        // 创建通道
        Channel channel = null;
        try {
            // 创建连接
            connection = factory.newConnection();
            // 创建通道
            channel = connection.createChannel();
            //声明交换机
            channel.exchangeDeclare(TOPIC_EXCHANGE, BuiltinExchangeType.TOPIC, true, false, null);
            // 声明队列
            channel.queueDeclare(TOPIC_QUEUE_1, true, false, false, null);
            channel.queueDeclare(TOPIC_QUEUE_2, true, false, false, null);

            DefaultConsumer consumer = new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    System.out.println(new String(body));
                }
            };
            channel.basicConsume(TOPIC_QUEUE_2, true, consumer);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
